

import java.sql.*;



public class SQLOperation {


    public void add(Object[] data,String DB){
        Connection con=Connect.getConn();
        try{
            String dataLine="";
            for(int i=0;i<data.length;i++){
                dataLine=dataLine+"'"+data[i]+"'";
                if(i!= data.length-1) dataLine=dataLine+",";
            }
            String SQL="INSERT INTO "+DB+" values("+dataLine+")";
            System.out.println(SQL);

            Statement SQLStatement=con.createStatement();

            SQLStatement.executeUpdate(SQL);


        }
        catch(Exception e){
            e.printStackTrace();
        }

    }

    public void modify(String DB,String conditionName, Object conditionData, String modifyAttri, Object modifyData){
        Connection con=Connect.getConn();
        try{
            String SQL="UPDATE "+DB+" SET "+modifyAttri+" = '"+modifyData+"' WHERE "+conditionName+" = '"+conditionData+"'";


            System.out.println(SQL);

            Statement SQLStatement=con.createStatement();

            SQLStatement.executeUpdate(SQL);

        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

    public Object[][] search(String DB,String[] selectAttri,String conditionName, Object conditionData){

        Connection con=Connect.getConn();
        Object[][] data;
         try{
            ResultSet result;
            String dataLine="";
             for(int i=0;i<selectAttri.length;i++){
                 dataLine=dataLine+selectAttri[i];
                 if(i!= selectAttri.length-1) dataLine=dataLine+",";
             }
             String SQL="SELECT "+dataLine+" FROM "+DB+" WHERE "+conditionName+"='"+conditionData+"' AND shanchu=1";
             System.out.println(SQL);
             //System.out.println(SQL);

             Statement SQLStatement=con.createStatement();

             ResultSet res=SQLStatement.executeQuery(SQL);

             int num=0;
             while(res.next()){
                 num++;
             }

             if(num!=0){
                data=new Object[num][selectAttri.length];
                //System.out.println(num);

                result=SQLStatement.executeQuery(SQL);
                int x=0;

                while(result.next()){
                    for(int i=0;i<selectAttri.length;i++){
                        data[x][i]=result.getObject(i+1);
                        //System.out.println(data[x][i]);
                    }
                    x++;
                }
                return data;
            }
            else{
                return new Object [0][selectAttri.length];
            }

        }catch(Exception e){
            e.printStackTrace();
            return new Object[0][0];
        }

    }

}
